<?php
class ComplainAction extends AdminPublicAction{
	/** =============
	 列表
	 =================**/
	public function index()
	{
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "r.report_id";
			$_GET['updown'] = $upDown = "desc";
	
		}
		$keyword = I('keyword','');
	
		//if(tep_not_null($keyword))$where['r.comment']  = array('like','%'.$keyword.'%');
		if(tep_not_null($keyword)){
			$map['p.products_name']  = array('like','%'.$keyword.'%');
			$map['r.comment']  = array('like','%'.$keyword.'%');
			$map['_logic'] = 'or';
			$where['_complex'] = $map;
			//$where['r.comment']  = array('like','%'.$keyword.'%');
		}
		$where['report_type'] = array('in','2,3');//商品举报
		$where['uid'] = array('gt',0);
	
		import("ORG.Util.Page");
		$page = 10;
		$count = D('ComplainView')->where($where)->count();
		$Page = new Page($count, $page);
		$list   = D('ComplainView')->getAll($where,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		$show = $Page->show();
	
			
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
		//var_dump($list);die;
		$this->keyword = $keyword;
		$this->listing = $list;
		$this->page = $show;
		$this->left_report = 'current';
		$this->display();
	
	}
	/** =============
	 添加 & 編輯
	 =================**/
	public function  detail(){
	
		$editId = I('id','',intval);
	
		if(tep_not_null($editId)&&is_numeric($editId)){
			$where['r.report_id']  = $editId;
			$where['r.reply_report_id']  = $editId;
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			/*編輯*/
			$list   = D('ComplainView')->getOneAll($map);
			//var_dump($list);die;
			$pname = M('membership')->where(array('uid'=>$list[0]['puid']))->field('name')->find();
			//echo M('membership')->getLastSql();die;
			//var_dump($pname);die;
			//echo $pname['name'];die;
			$this->listInfo = $list;
			$this->editId = $editId;
			$this->pname = $pname['name'];
			$this->buid = $list[0]['buid'];
			$this->puid = $list[0]['puid'];
			$this->action = 'editHandle';
			$this->actionName = 'Save';
		}else{
			$this->redirect('Report/index');
		}
	
		$this->left_report = 'current';
		$this->display();
	
	}
	
	/** =============
	 留言处理
	 =================**/
	public function editHandle()
	{
		if(IS_POST){
			//var_dump($_POST);die;
			$id = I('post.editId');
			$data['reply_report_id'] = $id;
			$send = I('post.send');
			if($send=='show'){
				$data['to_uid'] = I('post.puid');
			}else{
				$data['to_uid'] = I('post.buid');
			}
			$data['comment'] = I('post.comment');
			$data['report_date'] = date("Y-m-d H:i:s");
			M("report")->add($data);
			S('success','发送信息成功!');
			$this->redirect('index');
		}else{
			$this->redirect('index');
		}
	}
	//#############
	//处理举报状态
	//############
	public function dealed(){
		$id = I('id');
		$result = M('report')->where(array('report_id'=>$id))->setField('deal_flag', 1);
		if($result){
			S('success','举报设置处理成功!');
			$this->redirect('index');
		}else{
			S('err','举报设置处理失败!');
			$this->redirect('index');
		}
	}
	public function delete()
	{
		$id  = I('id');
		if(tep_not_null($id) && is_numeric($id)){
			$where['r.report_id']  = $editId;
			$where['r.reply_report_id']  = $editId;
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			M('report')->where($map)->delete();
			S('success','删除举报成功!');
			$this->redirect('index');
		}else{
			S('err','删除举报失败!');
			$this->redirect('index');
		}
	}
	
	
	public function changeStatus(){
		$check = M("news")->where(array("news_id" => (int)$_GET['id']))->getField('news_status');
		$news_status = ($check == 1)?0:1;
		M("news")->where(array("news_id" => (int)$_GET['id']))->setField('news_status',$news_status);
		echo (int)$news_status;
			
	}
	
}